package socketexercise;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    /**
     *
     */
   private ServerSocket serverSocket;
   private Socket socket;

    public Server(){
        try {
            System.out.println("正在启动服务器");
            serverSocket=new ServerSocket(8090);//向服务端申请服务端口，客户端就是通过该端口与服务器建立连接的
            System.out.println("服务器启动完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
        while(true){
            try {
                socket= serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                InputStream in=socket.getInputStream();//低级流
                InputStreamReader isr=new InputStreamReader(in);
                BufferedReader br=new BufferedReader(isr);
                String message;
                while((message=br.readLine())!=null){

                    System.out.println("客户端说："+message);
                }


            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        }




    public static void main(String[] args) {
        Server server=new Server();
        server.start();

    }
}
